//index.js
//获取应用实例
var app = getApp();
//console.log(app.globalData);
Page({
  data: {
    userInfo: {},
    imageList:[],
    imageKeys:[],
    topicType:2,
    pidData:{},
    topicData:{},
    videoSrc:'',
    videoKey:'',
    topic_id:0,
    setDate:0,
    orderListActive:0,
    chooseVideoText:'选择视频',
    status:0,
    content:'',
    starttime:'',
    endtime:'',
    enddate: '',
    pidindex:'',
    groupList:[],
    countArray:[1,2,3,4,5,6,7],

  },


  //设置时间
  bindDateChange: function (e) {
    var v = e.detail.value;
    var name = e.currentTarget.dataset.name;
   if(name=="enddate"){
     this.setData({ enddate: v });
   } 
    if (name == "starttime") {
      this.setData({ starttime: v });
    } 
    if (name == "endtime") {
      this.setData({ endtime: v });
    } 
    
  },

  bindStatusChange: function (e) {
    var v = Number(e.detail.value);
    this.setData({ status: v });
  },
  onShow:function(){
    
  },
  onHide:function(){
    app.setPageData("index/issue", { options: null });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.pid = options.pid||40;
    this.getPidData();
  },
   
  getTopic: function () {
    console.log("getTopic");
    var that = this;
    var pid = this.data.pid2;
    var topic_id = this.data.topic_id;
    var id = topic_id ? topic_id : pid;
    app.api("sns/topic/detail", {
      id: id,
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      that.setData({ topicData: res.data ,pid:res.data.pid});
      if (topic_id>0){
       // console.log("324234",res.data);
        that.setData({
          imageList: res.data.pics,
          imageKeys: res.data.imageKeys,
          videoSrc: res.data.video_url,
          videoKey: res.data.videoKey,
          content: res.data.title,
          status: Number(res.data.status)-1
         });
         if(res.data.pid2>0){
           that.setData({ topicType: 1});
         }
      }
    })
  },


  //
  getPidData: function () {
    var that = this;
    var pid = this.data.pid;
    app.api("sns/topic/detail", {
      id: pid
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      that.setData({ pidData: res.data });
    
    })
  },

  delImgVideo:function(){
    this.setData({
      videoSrc: '',
      videoKey: '',
    })
  },
  choose:function(){
    var that = this;
    console.log("videoSrc",this.data.videoSrc);
    if (this.data.videoSrc.length > 0 && this.data.imageKeys.length == 0){
      that.chooseImage();
      return ;
    }
    if (this.data.imageKeys.length>1){
      that.chooseImage();
      return ;
    }
    
    wx.showActionSheet({
      itemList: ['上传图片', '上传视频'],
      success: function (res) {
        if (res.tapIndex==0){
          that.chooseImage();
        } else if (res.tapIndex == 1){
          that.chooseVideo();
        }
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },
  chooseVideo: function () {
    var that = this;
    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      maxDuration: 60,
      camera: 'back',
      success: function (res) {
        var filePath = res.tempFilePath;
        that.setData({ chooseVideoText: '上传中...' });
        app.upload(filePath, (res) => {
          console.log(res);
          that.setData({ chooseVideoText: '选择视频' });
          that.setData({
            videoSrc: res.imageURL,
            videoKey:res.key,
          })
        })

      }
    })
  },

  chooseImage:function(){
    var that = this
    var imageList = this.data.imageList;
    var imageKeys = this.data.imageKeys;
    // if (this.data.topicType == 2) {
    //   var max = 3 - this.data.imageList.length;
    // } else {
    //   var max = 1;
    // }
    var max = 3 ;
    console.log(this.data.videoSrc);
    //上传了视频那只能上传一张图片 
    if (this.data.videoSrc.length > 0) {
      var max = 1;
    }
    wx.chooseImage({
      sizeType: 'compressed',
      count: max,
      success: function (res) {
        var newImages = res.tempFilePaths;
        for (var i in newImages){
          var filePath = newImages[i];
          app.upload(filePath, (res) => {
            imageList = imageList.concat(res.imageURL);
            imageKeys = imageKeys.concat(res.key) ;
            that.setData({ imageList: imageList, imageKeys: imageKeys});
          })
        }
      }
    })
  },
  delImg:function(e){
    var id = e.currentTarget.dataset.id;
    var imageList = this.data.imageList;
    var imageKeys = this.data.imageKeys;
    imageList.splice(id, 1);
    imageKeys.splice(id, 1);
    this.setData({ imageList: imageList, imageKeys: imageKeys});
  },
  //浏览图片
  previewImage: function (e) {
    var current = e.target.dataset.src
    wx.previewImage({
      current: current,
      urls: this.data.imageList
    })
  },

  formSubmit:function(e){
    var that = this;
    var user_info = app.globalData.userInfo;
    var postData = e.detail.value;
    if (this.data.loading == 1) {
      return;
    }
    //console.log(postData);
    //创建心愿单
    if (postData.title == "") {
      app.alert('请先输标题');
      return false;
    }
    
    if (postData.desc == "") {
      app.alert('请先输入内容');
      return false;
    }
    if (this.data.starttime == "" || this.data.endtime=="") {
      app.alert('请设置时间段');
      return false;
    }
    if (this.data.enddate == "") {
      app.alert('请选择过期时间');
      return false;
    }
    if (this.data.videoSrc.length > 0 && this.data.imageKeys.length == 0) {
      app.alert('添加视频封面图片');
      return false;
    }
    // if (postData.money>0 && !this.maxCount){
    //   app.alert('请设置打卡次数');
    //   return false;
    // }
    that.doformSubmit(e);
  },

  //发布活动 
  doformSubmit: function (e) {
  
    var user_info = app.globalData.userInfo;
    var postData = e.detail.value;
    if (this.data.loading==1){
      return ;
    }
    
    this.setData({ 'loading': 1 });
    this.setData({ 'disabled': 1 });
    var _this = this;
    var contentData = {   
      starttime: this.data.starttime,
      endtime: this.data.endtime,
      enddate: this.data.enddate
    };
    //提交回答
    app.api("sns/topic/add", {
      title: postData.title,
      desc: postData.desc,
      status: Number(this.data.status)+1,
      pics: this.data.imageKeys.join(","),
      password: postData.password,
      type:'faceEngTopic',
      pid:this.data.pid,
      pid2:0,
      video_url: this.data.videoKey,
      content: JSON.stringify(contentData),
      method: 'POST',
    }, function (res) {
   
      var title = "";
      if (res.code == 200) {
        _this.suc();
      } else {
        _this.setData({ 'loading': 0 });
        _this.setData({ 'disabled': 0 });
        app.alert(res.msg);
      }
    })
  },

 
  suc:function(){
    var _this =this;
    wx.showToast({
      title: '发布成功',
      icon: 'success',
      duration: 2000
    });
    app.setPageData("group/index", { reload: 1 });
    app.setPageData("user/timeline", { reload: 1 });
    _this.navigateBack();
  },
//返回 
  navigateBack: function (e){
    wx.switchTab({
      url: '/pages/index/index'
    });
  },


   

  
})
